<template>
  <span>
    <a-select @change="configChange" placeholder="请选择表单配置" style="width: 80%" showSearch :filter-option="filterOption">
      <a-select-option v-for="item in configList" :key="item.id" :value="item.id"> {{ item.name_ }} </a-select-option>
    </a-select>

  </span>
</template>
<script>
import { queryAct_dsn_form_defineList } from '@/api/workflow/act_dsn_form_define.js'
import { viewLayoutByStepId } from '@/api/workflow/act_dsn_form_layout.js'
export default {
  props: {
    echo: {
      type: String,
      default: 'form'
    }
  },
  data() {
    return {
      configList: [],
      queryParam: {
        pageSize: 9999999,
        pageNumber: 1,
        sort: 'lasttime',
        f_category_: '',
        f_name_: '',
        f_version_: 0
      } // 查询条件
    }
  },
  created() {
    this.getFormConfigList(this.queryParam)
  },
  methods: {
    // 下拉框搜索
    filterOption(input, option) {
      return option.componentOptions.children[0].text.indexOf(input) >= 0
    },
    // 选中配置
    configChange(formid) {
      viewLayoutByStepId({ formid: formid }).then(res => {
        // console.log('已配好的', res)
        if (res.code == 0) {
          let layoutJSON = JSON.parse(res.data.layout_ || {})
          console.log('已配好的', layoutJSON)
          // 默认回显表单配置
          if (this.echo == 'form') {
            if (layoutJSON?.formJson?.formObj && JSON.stringify(layoutJSON.formJson.formObj) != '{}') {
              this.$store.commit('setFormObj', layoutJSON.formJson.formObj)
              if (JSON.stringify(layoutJSON.formJson.formObj.LIST) != '{}') {
                this.$emit('refreshKey')
              }
              this.$message.success('回显成功')
            } else {
              this.$message.warning('该事项尚未配置')
            }
          } else if(this.echo == 'component') {
            // 回显组件配置
            if (layoutJSON?.componentJson?.analysisObj) {
              this.$store.commit('setOriginal', layoutJSON?.componentJson?.originalObj || null)
              this.$store.commit('setAnalysis', layoutJSON?.componentJson?.analysisObj || null)
              this.$emit('f_echoChange')
              this.$message.success('回显成功')
            } else {
              this.$message.warning('该事项尚未配置')
            }
          } else if(this.echo == 'tree') {
            if(layoutJSON.treeList && layoutJSON.treeList.length > 0) {
              this.$emit('f_echoChange', layoutJSON.treeList)
              this.$message.success('回显成功')
            } else {
              this.$message.warning('该事项尚未配置')
            }
          }
        }
      })
    },
    // 查询已有的表单配置
    getFormConfigList(queryParam) {
      queryAct_dsn_form_defineList(queryParam).then(res => {
        if (res.code == 0) {
          this.configList = res.data?.records || []
        }
      })
    }
  }
}
</script>

<style scoped></style>
